package graphicUserInterface.NetworkInterfaces;

import graphicUserInterface.NetworkInterfaces.screens.NetWorkScreen;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class NetworkTestWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2081486644765025777L;
	
	
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private JPanel _screenCommons;
	private JPanel _mainScreen;
	private JMenuBar _menuBar;
	private String _mainScreenPlace;
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	public NetworkTestWindow() {
		super();
		System.out.println("NetworkWindow Creation");
		this.setSize(800, 600);
		this.setTitle("Network Testing Tool :-)");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
		//Fst initialize the screen
		_screenCommons = initMainWindow();
		NetWorkScreen networking = new NetWorkScreen();
		addAsMainScreen(networking);
		this.setContentPane(_screenCommons);
		this.validate();
		System.out.println("CalibrationWindow Creation Over");
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */
	/**
	 * add the menuBar, menu entries and all the global stuff 
	 * @return A JPanel containing all the useful commons things
	 */
	private JPanel initMainWindow() {
		System.out.println("GUI:MainWindow  initMainWindows");
		//create the screen
		JPanel screen = new JPanel();
		screen.setLayout(new BorderLayout());
		screen.setBackground(Color.LIGHT_GRAY);
		//add a MenuBar
		_menuBar = new JMenuBar();
		JMenu HINBMenu = new JMenu();
		HINBMenu.setText("HINB");
		HINBMenu.add(new JMenuItem("test", 'e'));
		_menuBar.add(HINBMenu);
		screen.add(_menuBar, BorderLayout.NORTH);
		_mainScreenPlace = BorderLayout.CENTER;
		//add other commons displays
		//nothing to add now maybe in more advance version
		return screen;
	}
	/**
	 * remove the previous mainScreen, put the new one instead and 
	 * redraw the container.
	 * @param newMainScreen the new screen to show
	 */
	public synchronized void addAsMainScreen(JPanel newMainScreen) {
		System.out.println("GUI:MainWindow  addAsMainScreen");
		try {
			_screenCommons.remove(_mainScreen);
		} catch (NullPointerException e1) {
			System.out.println("no previous main screen nothing to " +
					"remove");
			// i.e. : _mainScreen is not Initialize yet
		}
		try{//we surround this by a try catch to be sure that a
			//programmer error will not crash the application :
			//i.e. if he/she give a bad name to _mainScreenPlace
			_screenCommons.add(newMainScreen, _mainScreenPlace);
		}catch (IllegalArgumentException e) {
			_screenCommons.add(newMainScreen);
		}finally{
			//no matter what happened, we need to redraw the screen
			_mainScreen = newMainScreen;
			_screenCommons.validate();
		}
	}
}
